<?php
	
		class Banners extends CI_Controller {
				
				function __construct(){
						parent::__construct();
				}
					
				function show() {
						$uri  = $this->uri->uri_to_assoc(2);
						$id   = @$uri['id'];
						
						if($id =='')
						{
								$this->output->set_status_header('404');
						}
						else
						{
								$banner = new Banner();
								$banner->where('id', (int)$id)
									   ->where('id >', 0)
									   ->get(1);
								if($banner->exists() && $banner->image && is_file('./images/banners/' . $banner->image))
								{
										$image = $banner->image;
										$hit = new Hit();
										$hit->where('h_added >', time()-24*60*60)
										    ->where('type', 'img')
											->where('ip', $this->input->ip_address())
											->get(1);
										if(!$hit->exists())
										{
												$banner->clear();
												$banner->where('id', (int)$id)
													   ->where('id >', 0)
													   ->update(array('impressions'=>'clicks+1'), FALSE);
										}
										
										$this->load->library('user_agent');
										$type 			= 'img';
										$hit->clear();
										$hit->item_id 	= $id;
										$hit->type  	= $type;
										$hit->ip  		= $this->input->ip_address();
										$hit->browser 	= $this->agent->browser();
										$hit->mobile  	= $this->agent->mobile();
										$hit->robot  	= $this->agent->robot();
										$hit->platform  = $this->agent->platform();
										$hit->referrer  = $this->agent->referrer();
										$hit->agent   	= $this->agent->agent_string();
										$hit->h_added  	= time();
										$hit->save();
										
										$info	  = pathinfo($image);
										$data 	  = @file_get_contents('./images/banners/' . $image);
										header('Content-type: ' . $this->_img_mime($info['extension']));
										die($data);
								}
						}
				}
				
				function _img_mime($ext) {
						$mimes = array(
							'jpeg'	=>	'image/jpeg',
							'jpg'	=>	'image/jpeg',
							'jpe'	=>	'image/jpeg',
							'png'	=>	'image/png',
						);
						return (isset($mimes[$ext]) ? $mimes[$ext] : 'image/jpeg');
				}
		}
		
?>